package fc.sj.study.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.zhy.http.okhttp.callback.StringCallback;

import butterknife.ButterKnife;
import butterknife.OnClick;
import fc.sj.study.R;
import fc.sj.study.utils.LogUtil;
import fc.sj.study.utils.OkHttpUtil;
import fc.sj.study.utils.ToastUtil;
import io.rong.imkit.RongIM;
import io.rong.imlib.RongIMClient;
import io.rong.imlib.model.Conversation;
import okhttp3.Call;

import static fc.sj.study.MApplication.getCurProcessName;

public class RyIMActivity extends AppCompatActivity {
    private String testtoken = "0RDIlsoJAIBbRofFRJtM18irdweIJWnjGaFelz46gflAcMTN7w9xcQ8kJB/sb8x7xk87DpLlo/SBfFbX6QNSbQ==";
    private String testtoken2 = "WsKDEBjOEQsIb23m1SMcTcirdweIJWnjGaFelz46gflAcMTN7w9xcWi9qAKhqneMB2y0k2rz4lzy/Gh8CEv8Xg==";
    private int a=-1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ry_im);
        ButterKnife.bind(this);
        Http();
//        connect(testtoken);
    }

    private void Http() {
        OkHttpUtil.GetToken(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                LogUtil.e("TAG", e+"");
            }

            @Override
            public void onResponse(String response, int id) {
                LogUtil.e("TAG", response);
            }
        });
    }

    private void connect(String token) {
        if (getApplicationInfo().packageName.equals(getCurProcessName(getApplicationContext()))) {

            RongIMClient.connect(token, new RongIMClient.ConnectCallback() {

                /**
                 * Token 错误。可以从下面两点检查 1.  Token 是否过期，如果过期您需要向 App Server 重新请求一个新的 Token
                 *                            2.  token 对应的 appKey 和工程里设置的 appKey 是否一致
                 */
                @Override
                public void onTokenIncorrect() {
                    ToastUtil.showToast("失败:token异常");
                    LogUtil.e("TAG", "失败:token异常");
                }

                /**
                 * 连接融云成功
                 * @param userid 当前 token 对应的用户 id
                 */
                @Override
                public void onSuccess(String userid) {
                    ToastUtil.showToast("成功");
                    if(a==0){
                        RongIM.getInstance().startPrivateChat(RyIMActivity.this, "1000", "我的私人聊天");
                    } else if(a==1){
                        if (RongIM.getInstance() != null)
                            RongIM.getInstance().startSubConversationList(RyIMActivity.this, Conversation.ConversationType.PRIVATE);
//                        RongIM.getInstance().startPrivateChat(RyIMActivity.this, "1234", "我的私人聊天");
                    }
                }

                /**
                 * 连接融云失败
                 * @param errorCode 错误码，可到官网 查看错误码对应的注释
                 */
                @Override
                public void onError(RongIMClient.ErrorCode errorCode) {
                    ToastUtil.showToast("失败:" + errorCode);
                    LogUtil.e("TAG", "" + errorCode);
                }
            });
        }
    }

    @OnClick({R.id.one, R.id.two,R.id.three,R.id.four})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.one:
                connect(testtoken);
                a=0;
                break;
            case R.id.two:
                connect(testtoken2);
                a=1;
                break;
            case R.id.three:
//                RongIM.getInstance().cre
                break;
            case R.id.four:
                break;
        }
    }
}
